let map_enable = true;
let map_adcode = 500000;
let map_center = [107.873859,30.20046];
let map_zoom = 7;
/**---------- 地图 --------------------------*/
var map = new AMap.Map("map_mobile", {
    zoom: map_zoom, //地图级别
    center: map_center, //地图中心
    mapStyle: 'amap://styles/blue',
    rotateEnable: map_enable,
    dragEnable: map_enable,
    scrollWheel: map_enable,
    doubleClickZoom: map_enable
});
/**----------- 行政区浏览 ------------------*/
AMapUI.load(['ui/geo/DistrictExplorer', 'lib/$'], function (DistrictExplorer, $) {
    //创建一个实例
    let districtExplorer = window.districtExplorer = new DistrictExplorer({
        eventSupport: true, //打开事件支持
        map: map
    });
    let node_all;
    //当前聚焦的区域
    let currentAreaNode = null;

    let $tipMarkerContent = $('<div style="color:white;font-size:3px"></div>');

    let tipMarker = new AMap.Marker({
        content: $tipMarkerContent.get(0),
        offset: new AMap.Pixel(0, 0),
        bubble: true,
        zIndex: 99
    });

    //根据Hover状态设置相关样式
    function toggleHoverFeature(feature, isHover, position) {

        tipMarker.setMap(isHover ? map : null);

        if (!feature) {
            return;
        }

        let props = feature.properties;

        if (isHover) {

            //更新提示内容
            $tipMarkerContent.html(props.name);
            //更新位置
            tipMarker.setPosition(position || props.center);
        }

        $('#area-tree').find('h2[data-adcode="' + props.adcode + '"]').toggleClass('hover', isHover);

        //更新相关多边形的样式
        let polys = districtExplorer.findFeaturePolygonsByAdcode(props.adcode);
        for (let i = 0, len = polys.length; i < len; i++) {

            polys[i].setOptions({
                fillOpacity: isHover ? 0.5 : 0.2
            });
        }
    }

    //监听feature的hover事件
    districtExplorer.on('featureMouseout featureMouseover', function (e, feature) {
        toggleHoverFeature(feature, e.type === 'featureMouseover',
            e.originalEvent ? e.originalEvent.lnglat : null);
    });

    //监听鼠标在feature上滑动
    districtExplorer.on('featureMousemove', function (e) {
        //更新提示位置
        tipMarker.setPosition(e.originalEvent.lnglat);
    });

    //绘制某个区域的边界
    function renderAreaPolygons(areaNode) {
        node_all = areaNode;
        //清除已有的绘制内容
        districtExplorer.clearFeaturePolygons();

        //绘制子区域
        districtExplorer.renderSubFeatures(areaNode, function () {

            // let fillColor = colors[i % colors.length];
            // let strokeColor = colors[colors.length - 1 - i % colors.length];
            let strokeColor = "#FFFFFF";

            return {
                cursor: 'default',
                bubble: true,
                strokeColor: strokeColor, //线颜色
                strokeOpacity: 1, //线透明度
                strokeWeight: 1, //线宽
                fillColor: null, //填充色
                fillOpacity: 0.35, //填充透明度
            };
        });

        //绘制父区域
        districtExplorer.renderParentFeature(areaNode, {
            cursor: 'default',
            bubble: true,
            strokeColor: '#FFFFFF', //线颜色
            strokeOpacity: 1, //线透明度
            strokeWeight: 1, //线宽
            fillColor: null, //填充色
            fillOpacity: 0.35, //填充透明度
        });
    }

    //切换区域后刷新显示内容
    function refreshAreaNode(areaNode) {

        districtExplorer.setHoverFeature(null);

        renderAreaPolygons(areaNode);

        //更新选中节点的class
        let $nodeEles = $('#area-tree').find('h2');

        $nodeEles.removeClass('selected');

        let $selectedNode = $nodeEles.filter('h2[data-adcode=' + areaNode.getAdcode() + ']').addClass('selected');

        //展开下层节点
        $selectedNode.closest('li').removeClass('hide-sub');

        //折叠下层的子节点
        $selectedNode.siblings('ul.sublist').children().addClass('hide-sub');
    }

    //切换区域
    function switch2AreaNode(adcode, callback) {

        if (currentAreaNode && ('' + currentAreaNode.getAdcode() === '' + adcode)) {
            return;
        }

        loadAreaNode(adcode, function (error, areaNode) {

            if (error) {

                if (callback) {
                    callback(error);
                }

                return;
            }

            currentAreaNode = window.currentAreaNode = areaNode;

            //设置当前使用的定位用节点
            districtExplorer.setAreaNodesForLocating([currentAreaNode]);

            refreshAreaNode(areaNode);

            if (callback) {
                callback(null, areaNode);
            }
        });
    }

    //加载区域
    function loadAreaNode(adcode, callback) {

        districtExplorer.loadAreaNode(adcode, function (error, areaNode) {

            if (error) {

                if (callback) {
                    callback(error);
                }

                console.error(error);

                return;
            }

            if (callback) {
                callback(null, areaNode);
            }
        });
    }

    //重庆
    switch2AreaNode(map_adcode);
});
var pathSimplifierIns;
AMapUI.load(['ui/misc/PathSimplifier'], function (PathSimplifier) {
    if (!PathSimplifier.supportCanvas) {
        alert('当前环境不支持 Canvas！');
        return;
    }
    //创建组件实例
    pathSimplifierIns = new PathSimplifier({
        zIndex: 100,
        map: map, //所属的地图实例
        getPath: function (pathData) {
            //返回轨迹数据中的节点坐标信息，[AMap.LngLat, AMap.LngLat...] 或者 [[lng|number,lat|number],...]
            return pathData.path;
        },
        getHoverTitle: function (pathData, pathIndex, pointIndex) {
            //返回鼠标悬停时显示的信息
            if (pointIndex >= 0) {
                //鼠标悬停在某个轨迹节点上
                return '点:' + pointIndex + '/' + pathData.path.length + "时间:" + pathData.time[pointIndex];
            }
            //鼠标悬停在节点之间的连线上
            return '点数量' + pathData.path.length;
        },
        renderOptions: {
            //轨迹线的样式
            pathLineStyle: {
                strokeStyle: 'yellow',
                lineWidth: 6,
                dirArrowStyle: true
            },
            keyPointTolerance: 1
        }
    });
});

//
function initPage(pathSimplifierIns, location_list, time_list) {

    //这里构建两条简单的轨迹，仅作示例
    pathSimplifierIns.setData([{
        name: '轨迹0',
        path: location_list,
        // time: time_list
    }]);

    //创建一个巡航器
    let navg0 = pathSimplifierIns.createPathNavigator(0, //关联第1条轨迹
        {
            loop: true, //循环播放
            speed: location_list.length * 100,
        });

    navg0.start();
}


// function path(location_list, time_list) {
//     pathSimplifierIns.clearPathNavigators();
//     //启动页面
//     initPage(pathSimplifierIns, location_list, time_list);
// }

function path(location_list) {
    pathSimplifierIns.clearPathNavigators();
    //启动页面
    initPage(pathSimplifierIns, location_list);
}
var colors = {};
var getColorBySoc = function (adcode) {
    if (adcode) {
        if (!colors[adcode]) {
            if (adcode === map_adcode) {
                return null;
            }
            return '#033447'
            // return '#FFFFFF'
        }
    } else {
        return null
    }
};
var disCountry = new AMap.DistrictLayer.Country({
    zIndex: 19,
    SOC: 'CHN',
    depth: 1,
    styles: {
        'fill': function (props) {
            return getColorBySoc(props.adcode_pro)
        }
    }
});
map.add(disCountry);

// ----------------------------------------------------------
map.on("moveend", function (e) {
    console.log(map.getCenter());
});
// ************* 缩放获取当前地图缩放级别 ******************
map.on("zoomend", function () {
    console.log(map.getZoom());
});